package com.wis.service.impl;

import com.wis.entity.BannerEntity;
import com.wis.entity.NoticeEntity;
import com.wis.mapper.NoticeMapper;
import com.wis.service.NoticeService;
import com.wis.util.CommonResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Resource
    private NoticeMapper noticeMapper;
    @Override
    public CommonResult showNotice(int page, int limit) {
        int pageTemp = (page - 1) * limit;
        if (pageTemp < 0) {
            System.out.println(1);
            return new CommonResult(400, "页数不能为负数", 0, null);
        }
        List<NoticeEntity> noticeEntities = noticeMapper.showNotice(pageTemp, limit);
        int count = noticeMapper.showNoticeCount();

        return CommonResult.build(200, "ok", count, noticeEntities);
    }

    @Override
    public CommonResult showOneNotice(int id) {
        return null;
    }

    /**
     * 模糊查询公告
     * **/
    @Override
    public CommonResult findBlurry(String name) {
        List<NoticeEntity> noticeEntities = noticeMapper.findBlurry(name);
        return CommonResult.build(200, "ok", noticeEntities.size(), noticeEntities);
    }

    @Override
    public CommonResult delNotice(int id) {
        // 调用Mapper删除数据
        int i = noticeMapper.delNotice(id);
        if (i > 0) {  // 删除成功
            return CommonResult.build(200, "ok", i, id);
        }
        // 删除失败
        return CommonResult.build(500, "no", i, id);
    }

    @Override
    public CommonResult updateNotice(NoticeEntity notice) {
        // 调用Mapper更新数据
        int i = noticeMapper.updateNotice(notice);
        if (i > 0) {  // 更新成功
            return CommonResult.build(200, "ok", i, notice);
        }
        // 更新失败
        return CommonResult.build(500, "no", i, notice);
    }

    @Override
    public CommonResult addNotice(NoticeEntity notice) {
        // 调用Mapper插入数据
        int i = noticeMapper.addNotice(notice);
        if (i > 0) {  // 插入成功
            return CommonResult.build(200, "ok", i,notice);
        }
        // 插入失败
        return CommonResult.build(500, "no", i, notice);
    }
}
